package cn.tp.ksoft.civil_driver.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.BrandVo;
import cn.tp.ksoft.civil_driver.bean.CarType;
import cn.tp.ksoft.civil_driver.bean.EnterpriseDto;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.request.RequestModules;
import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.civil_driver.view.simple.SpaceItemDecoration;

/**
 * Created by zhusong on 16/5/2.
 */
public class RadioItemSelectActivity extends DriverBaseActivity {

    private String TYPE = "";
    private BrandVo mBrand;
    private DriverRequest request;

    private RecyclerView mRv;
    private RadioSelectAdapter mAdapter;
    // actual data
    private SparseArray<EnterpriseDto> mEnterprise;
    private SparseArray<CarType> mCarTypes;
    private SparseArray<BrandVo> mBrandModels;

    private SparseArray<String> mRadioTextData;// map to actual data display text
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_radio_select);
        updateType();

        request = new DriverRequest(this, new WeakHandler(this));
        mRv = (RecyclerView) findViewById(R.id.rv);
        mAdapter = new RadioSelectAdapter();
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mRv.setAdapter(mAdapter);
        mRv.addItemDecoration(new SpaceItemDecoration(Math.round(getResources().getDisplayMetrics().density * 1f)));
        if (TYPE.equals(Constants.VALUE_RADIO_CAR_COLOR)) {
            updateTitle(getString(R.string.driver_car_color));
            String[] colors = {"红色", "黄色", "香槟色", "银色", "黑色", "白色", "绿色"};
            mRadioTextData = new SparseArray<>(colors.length);
            for (int i = 0, len = colors.length; i < len; i++) {
                mRadioTextData.append(i, colors[i]);
            }
            mAdapter.notifyDataSetChanged();
        } else if (TYPE.equals(Constants.VALUE_RADIO_OWNER)) {
            updateTitle(getString(R.string.driver_car_owner));
            request.getEnterprise();
        } else if (TYPE.equals(Constants.VALUE_RADIO_CAR_TYPE)) {
            updateTitle(getString(R.string.driver_car_type));
            mCarTypes = new SparseArray<>(3);
            mCarTypes.append(0, new CarType(1, "轿车"));
            mCarTypes.append(1, new CarType(2, "商务车"));
            mCarTypes.append(2, new CarType(3, "客车"));
            mRadioTextData = new SparseArray<>();
            mRadioTextData.put(0, mCarTypes.get(0).getTypeName());
            mRadioTextData.put(1, mCarTypes.get(1).getTypeName());
            mRadioTextData.put(2, mCarTypes.get(2).getTypeName());
            mAdapter.notifyDataSetChanged();
        } else if (TYPE.equals(Constants.VALUE_RADIO_BRAND_TYPE)) {
            updateTitle(getString(R.string.driver_car_brand_model));
            request.getBrandModelList(mBrand.getId());
        }

    }

    @Override
    public void onBackPressed() {
        int selectIndex = mAdapter.getSelectIndex();
        if (selectIndex >= 0) {
            Intent data = new Intent();
            if (TYPE.equals(Constants.VALUE_RADIO_CAR_TYPE)) {
                data.putExtra(Constants.KEY_RADIO_DATA, mCarTypes.get(selectIndex));
            } else if(TYPE.equals(Constants.VALUE_RADIO_CAR_COLOR)) {
                data.putExtra(Constants.KEY_RADIO_DATA, mRadioTextData.get(selectIndex));
            } else if(TYPE.equals(Constants.VALUE_RADIO_BRAND_TYPE)) {
                data.putExtra(Constants.KEY_RADIO_DATA, mRadioTextData.get(selectIndex));
            } else if (TYPE.equals(Constants.VALUE_RADIO_OWNER)) {
                data.putExtra(Constants.KEY_RADIO_DATA, mEnterprise.get(selectIndex));
            }
            setResult(RESULT_OK, data);
        }
        super.onBackPressed();
    }

    private void updateType() {
        Intent intent = getIntent();
        if (null != intent) {
            TYPE = intent.getStringExtra(Constants.KEY_RADIO_TYPE);
            mBrand = intent.getParcelableExtra(Constants.KEY_BRAND);
        }
    }

    class RadioHolder extends RecyclerView.ViewHolder {
        TextView mTv;
        ImageView mSelectIv;
        public RadioHolder(View itemView) {
            super(itemView);
            mTv = (TextView) itemView.findViewById(R.id.tv);
            mSelectIv = (ImageView) itemView.findViewById(R.id.iv_select);
        }
    }

    class RadioSelectAdapter extends RecyclerView.Adapter<RadioHolder> {

        private int mSelectIndex = -1;

        public int getSelectIndex() {
            return mSelectIndex;
        }
        @Override
        public RadioHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.driver_radio_select_item, viewGroup, false);
            return new RadioHolder(view);
        }

        @Override
        public void onBindViewHolder(final RadioHolder radioHolder, int position) {
            String text = mRadioTextData.get(position);
            radioHolder.mTv.setText(text);
            radioHolder.mSelectIv.setVisibility(mSelectIndex == position ? View.VISIBLE : View.GONE );
            radioHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int preIndex = mSelectIndex;
                    mSelectIndex = radioHolder.getAdapterPosition();
                    if (preIndex >= 0) {
                        notifyItemChanged(preIndex);
                    }
                    notifyItemChanged(mSelectIndex);
                }
            });
        }

        @Override
        public int getItemCount() {
            return null != mRadioTextData ? mRadioTextData.size() : 0;
        }
    }


    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {
            case RequestModules.GETENTERPRISE : {
                List<EnterpriseDto> list = (List<EnterpriseDto>) result.getData();
                if (null == list) {
                    return;
                }
                // update actual data and text data
                mEnterprise = new SparseArray<>();
                mRadioTextData = new SparseArray<>(list.size());
                for (int i = 0, len = list.size(); i < len; i++) {
                    EnterpriseDto enterprise = list.get(i);
                    mEnterprise.append(i, enterprise);
                    mRadioTextData.append(i, enterprise.getName());
                }
                mAdapter.notifyDataSetChanged();
                break;
            }

            case RequestModules.GETBRANDMODELLIST : {
                List<BrandVo> list = (List<BrandVo>)result.getData();
                if (null == list) {
                    return;
                }
                mBrandModels = new SparseArray<>();
                mRadioTextData = new SparseArray<>(list.size());
                for (int i = 0, len = list.size(); i < len; i++) {
                    BrandVo brand = list.get(i);
                    mBrandModels.append(i, brand);
                    mRadioTextData.append(i, brand.getName());
                }
                mAdapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    protected void handleFailureResult(int what, @NonNull ResultBean result) {
        if (what == RequestModules.GETBRANDMODELLIST) {
            Toast.makeText(this, "获取型号失败", Toast.LENGTH_SHORT).show();
        }
    }
}
